package product

import (
	"context"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/sagoo-cloud/sagooiot/api/v1/product"
	"github.com/sagoo-cloud/sagooiot/internal/model"
	"github.com/sagoo-cloud/sagooiot/internal/service"
)

var UpgradeInfo = cProductUpgrade_info{}

type cProductUpgrade_info struct{}

// GetUpgradeInfoList 获取列表
func (u *cProductUpgrade_info) GetUpgradeInfoList(ctx context.Context, req *product.GetUpgradeInfoListReq) (res *product.GetUpgradeInfoListRes, err error) {
	var reqData = new(model.GetUpgradeInfoListInput)
	gconv.Scan(req, &reqData)
	total, currentPage, dataList, err := service.ProductUpgradeInfo().GetUpgradeInfoList(ctx, reqData)
	res = new(product.GetUpgradeInfoListRes)
	gconv.Scan(dataList, &res.Data)
	res.PaginationRes.Total = total
	res.PaginationRes.CurrentPage = currentPage
	return
}

// GetUpgradeInfoById 获取指定ID数据
func (u *cProductUpgrade_info) GetUpgradeInfoById(ctx context.Context, req *product.GetUpgradeInfoByIdReq) (res *product.GetUpgradeInfoByIdRes, err error) {
	data, err := service.ProductUpgradeInfo().GetUpgradeInfoById(ctx, req.Id)
	res = new(product.GetUpgradeInfoByIdRes)
	gconv.Scan(data, &res)
	return
}

// AddUpgradeInfo 添加数据
func (u *cProductUpgrade_info) AddUpgradeInfo(ctx context.Context, req *product.AddUpgradeInfoReq) (res *product.AddUpgradeInfoRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.UpgradeInfoAddInput{}
	gconv.Scan(req, &data)
	// data.CreatedBy = userInfo.Id
	err = service.ProductUpgradeInfo().AddUpgradeInfo(ctx, data)
	return
}

// EditUpgradeInfo 修改数据
func (u *cProductUpgrade_info) EditUpgradeInfo(ctx context.Context, req *product.EditUpgradeInfoReq) (res *product.EditUpgradeInfoRes, err error) {
	userInfo := service.Context().GetLoginUser(ctx)
	if userInfo == nil {
		err = gerror.New("未登录或TOKEN失效,请重新登录")
		return
	}
	var data = model.UpgradeInfoEditInput{}
	gconv.Scan(req, &data)
	// data.UpdateBy = userInfo.Id //如果需要保存信息，把这个打开
	err = service.ProductUpgradeInfo().EditUpgradeInfo(ctx, data)
	return
}

// DeleteUpgradeInfo 删除数据
func (u *cProductUpgrade_info) DeleteUpgradeInfo(ctx context.Context, req *product.DeleteUpgradeInfoReq) (res *product.DeleteUpgradeInfoRes, err error) {
	if len(req.Ids) == 0 {
		err = gerror.New("ID参数错误")
	}
	err = service.ProductUpgradeInfo().DeleteUpgradeInfo(ctx, req.Ids)
	return
}
